package com.liziba.trend.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liziba.trend.api.IndexData;
import com.liziba.trend.mapper.FundEquityMapper;
import com.liziba.trend.pojo.FundEquity;
import com.liziba.trend.pojo.FundEquityCrawling;
import com.liziba.trend.service.FundEquityService;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author liziba
 * @since 2021-02-04
 */
@Service
public class FundEquityServiceImpl extends ServiceImpl<FundEquityMapper, FundEquity> implements FundEquityService {


    /**
     * 将基金爬取数据转换为基金净值
     * @param fundEquities
     * @return
     */
    public static List<IndexData> convertFundEquityToIndexData(List<FundEquityCrawling> fundEquities) {
        // 指数数据集合
        List<IndexData> indexDataList = new ArrayList<>(fundEquities.size());
        if(fundEquities != null) {
            // 反转list -- 日期爬取是反的
            Collections.reverse(fundEquities);
            fundEquities.forEach(fundEquity -> {
                // 日期
                String date = fundEquity.getFSRQ();
                // 净值
                String closePoint = fundEquity.getDWJZ();
                // 创建指数数据
                indexDataList.add(new IndexData(date, Convert.toFloat(closePoint)));
            });
        }
        return indexDataList;
    }


    /**
     *
     * @param pageIndex     起始位置 1
     * @param pageSize      分页大学 固定20
     * @param startTime     开始时间 "2012-1-1";
     * @param endTime       结束时间 "2020-4-15";
     * @param code          基金代码
     * @return
     */
    public static LinkedList<FundEquityCrawling> crawlingFundEquityInfoMethod(int pageIndex, int pageSize, String startTime, String endTime, String code){
        String referer = "http://fundf10.eastmoney.com/f10/jjjz_" + code + ".html";
        long time = System.currentTimeMillis();
        String url = "http://api.fund.eastmoney.com/f10/lsjz?callback=jQuery18306596328894644803_1571038362181&" +
                "fundCode=%s&pageIndex=%s&pageSize=%s&startDate=%s&endDate=%s&_=%s";
        url = String.format(url,code,pageIndex,pageSize,startTime,endTime,time);
        HttpRequest request = HttpUtil.createGet(url);
        request.header("Referer", referer);
        String str = request.execute().body();
        // 首个(位置
        int firstIndexOf = str.indexOf("(");
        // 最后一个
        int lastIndexOf = str.lastIndexOf(")");
        //截取字符串
        str = str.substring(firstIndexOf + 1, lastIndexOf);
        //转换为Obj类型
        JSONObject jsonObject = JSON.parseObject(str);
        //获取数组
        JSONArray jsonArray = jsonObject.getJSONObject("Data").getJSONArray("LSJZList");
        if(jsonArray != null) {
            return JSON.parseObject(jsonArray.toJSONString(), new TypeReference<LinkedList<FundEquityCrawling>>(){});
        }
        return new LinkedList<FundEquityCrawling>();
    }
}
